// zTree插件配置参数
var setting = {
  view: {
    showIcon: false, //节点图标
  },
  edit: {
    drag: {
      autoExpandTrigger: true, //设置拖拽时父节点自动展开触发 onExpand 事件回调函数
      prev: dropPrev, //禁止拖拽到节点前面的操作
      inner: dropInner, //禁止拖拽成为目标节点的子节点
      next: dropNext //禁止拖拽到节点后面的操作
    },
    enable: true, //设置 zTree 进入编辑状态
    showRemoveBtn: false, //不显示删除按钮
    showRenameBtn: false //不显示编辑名称按钮
  },
  check: {
    enable: true, //需要显示 checkbox
  },
  data: {
    simpleData: {
      enable: true //使用简单 Array 格式的数据
    }
  },
  callback: {
    beforeDrag: beforeDrag, //禁止全部拖拽操作
    beforeDrop: beforeDrop, //禁止将节点拖拽成为根节点
    beforeDragOpen: beforeDragOpen, //禁止全部拖拽时的自动展开操作
    onDrag: onDrag, //每次开始进行拖拽节点后， 弹出被拖拽节点的个数信息
    onDrop: onDrop, //每次拖拽操作结束后， 弹出该被拖拽节点的个数以及目标节点的 tId、name 的信息
    beforeRename: beforeRename, //禁止修改的名称前的操作
    beforeClick: beforeClick //节点选中操作
  }
};
// 标准节点数据
var zNodes = [{
    id: 1,
    pId: 0,
    name: "战略类项目标准动作",
    open: true,
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 11,
    pId: 1,
    name: "OKR推行",
    open: false,
    childOuter: false,
    drag: false,
    dropInner: false
  },
  {
    id: 111,
    pId: 11,
    name: "每个季度OKR辅导",
    drag: false,
    dropInner: false,
    nocheck: true
  },

  {
    id: 2,
    pId: 0,
    name: "销售类项目标准动作",
    open: true,
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 12,
    pId: 2,
    name: "明确线索信息",
    open: false,
    childOuter: false,
    drag: false,
    dropInner: false
  },
  {
    id: 121,
    pId: 12,
    name: "获取线索信息和客户诉求",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 122,
    pId: 12,
    name: "获取客户计划和预算",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 123,
    pId: 12,
    name: "获取竞争信息",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 124,
    pId: 12,
    name: "获取项目关键决策链和关键人员",
    drag: false,
    dropInner: false,
    nocheck: true
  },

  {
    id: 22,
    pId: 2,
    name: "解决方案确定",
    open: false,
    childOuter: false,
    drag: false,
    dropInner: false
  },
  {
    id: 221,
    pId: 22,
    name: "获取客户需求",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 222,
    pId: 22,
    name: "完成解决方案设计和沟通",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 223,
    pId: 22,
    name: "更新客户计划和预算以及竞争信息",
    drag: false,
    dropInner: false,
    nocheck: true
  },

  {
    id: 32,
    pId: 2,
    name: "商务和合同落地",
    open: false,
    childOuter: false,
    drag: false,
    dropInner: false
  },
  {
    id: 321,
    pId: 32,
    name: "商务设计和报价（含法务）",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 322,
    pId: 32,
    name: "确定交付范围和时间计划",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 323,
    pId: 32,
    name: "确定版本交付功能",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 324,
    pId: 32,
    name: "更新客户计划和预算以及竞争信息",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 325,
    pId: 32,
    name: "合同签订",
    drag: false,
    dropInner: false,
    nocheck: true
  },

  {
    id: 42,
    pId: 2,
    name: "交付",
    open: false,
    childOuter: false,
    drag: false,
    dropInner: false
  },
  {
    id: 421,
    pId: 42,
    name: "设备到货和安装",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 422,
    pId: 42,
    name: "上电自测和问题解决",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 423,
    pId: 42,
    name: "初验和问题解决",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 424,
    pId: 42,
    name: "终验和问题解决",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 425,
    pId: 42,
    name: "问题和需求处理",
    drag: false,
    dropInner: false,
    nocheck: true
  },

  {
    id: 52,
    pId: 2,
    name: "回款",
    open: false,
    childOuter: false,
    drag: false,
    dropInner: false
  },
  {
    id: 521,
    pId: 52,
    name: "维保",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 522,
    pId: 52,
    name: "到货款",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 523,
    pId: 52,
    name: "初验款",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 524,
    pId: 52,
    name: "终验款",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 525,
    pId: 52,
    name: "尾款",
    drag: false,
    dropInner: false,
    nocheck: true
  },

  {
    id: 3,
    pId: 0,
    name: "事务类项目标准动作",
    open: true,
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 13,
    pId: 3,
    name: "降成本、规范运作、提升效率",
    open: false,
    childOuter: false,
    drag: false,
    dropInner: false
  },
  {
    id: 131,
    pId: 13,
    name: "费用降低",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 132,
    pId: 13,
    name: "效率提升改进",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 133,
    pId: 13,
    name: "内部流程和配合改进",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 134,
    pId: 13,
    name: "例会和周报运作规范性",
    drag: false,
    dropInner: false,
    nocheck: true
  },

  {
    id: 4,
    pId: 0,
    name: "部门类项目标准动作",
    open: true,
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 14,
    pId: 4,
    name: "降成本、提升效率",
    open: false,
    childOuter: false,
    drag: false,
    dropInner: false
  },
  {
    id: 141,
    pId: 14,
    name: "费用降低",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 142,
    pId: 14,
    name: "合理化建议贡献",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 143,
    pId: 14,
    name: "效率提升改进",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 144,
    pId: 14,
    name: "内部流程和配合改进",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 145,
    pId: 14,
    name: "新系统使用",
    drag: false,
    dropInner: false,
    nocheck: true
  },

  {
    id: 24,
    pId: 4,
    name: "团队建设和人员管理",
    open: false,
    childOuter: false,
    drag: false,
    dropInner: false
  },
  {
    id: 241,
    pId: 24,
    name: "例会和周报运作规范性",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 242,
    pId: 24,
    name: "完成公司摊派指标任务",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 243,
    pId: 24,
    name: "规章制度规范遵循",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 244,
    pId: 24,
    name: "内部交流和学习",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 245,
    pId: 24,
    name: "人员培养",
    drag: false,
    dropInner: false,
    nocheck: true
  },

  {
    id: 5,
    pId: 0,
    name: "测试类项目标准动作",
    open: true,
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 15,
    pId: 5,
    name: "方案确定和准备",
    open: false,
    childOuter: false,
    drag: false,
    dropInner: false
  },
  {
    id: 151,
    pId: 15,
    name: "费用降低",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 152,
    pId: 15,
    name: "合理化建议贡献",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 153,
    pId: 15,
    name: "效率提升改进",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 154,
    pId: 15,
    name: "内部流程和配合改进",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 155,
    pId: 15,
    name: "新系统使用",
    drag: false,
    dropInner: false,
    nocheck: true
  },

  {
    id: 25,
    pId: 5,
    name: "研发准备",
    open: false,
    childOuter: false,
    drag: false,
    dropInner: false
  },
  {
    id: 251,
    pId: 25,
    name: "测试用例准备",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 252,
    pId: 25,
    name: "测试用例确认和获取",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 253,
    pId: 25,
    name: "研发内部开发",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 254,
    pId: 25,
    name: "公司内完成实验室测试",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 255,
    pId: 25,
    name: "硬件和相关资源准备",
    drag: false,
    dropInner: false,
    nocheck: true
  },

  {
    id: 35,
    pId: 5,
    name: "安装和对接",
    open: false,
    childOuter: false,
    drag: false,
    dropInner: false
  },
  {
    id: 351,
    pId: 35,
    name: "到货",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 352,
    pId: 35,
    name: "软硬件安装",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 353,
    pId: 35,
    name: "上电自测和现网系统对接",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 354,
    pId: 35,
    name: "验证和问题解决",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 355,
    pId: 35,
    name: "现网割接",
    drag: false,
    dropInner: false,
    nocheck: true
  },

  {
    id: 45,
    pId: 5,
    name: "交付",
    open: false,
    childOuter: false,
    drag: false,
    dropInner: false
  },
  {
    id: 451,
    pId: 45,
    name: "设备到货和安装",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 452,
    pId: 45,
    name: "上电自测和问题解决",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 453,
    pId: 45,
    name: "初验和问题解决",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 454,
    pId: 45,
    name: "终验和问题解决",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 455,
    pId: 45,
    name: "问题和需求处理",
    drag: false,
    dropInner: false,
    nocheck: true
  },

  {
    id: 99,
    pId: 0,
    name: "自定义标准动作",
    open: true,
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 100,
    pId: 99,
    name: "自定义目标",
    open: false,
    childOuter: false,
    drag: false,
    dropInner: false
  },
  {
    id: 1001,
    pId: 100,
    name: "自定义任务1",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 1002,
    pId: 100,
    name: "自定义任务2",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 1003,
    pId: 100,
    name: "自定义任务3",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 1004,
    pId: 100,
    name: "自定义任务4",
    drag: false,
    dropInner: false,
    nocheck: true
  },
  {
    id: 1005,
    pId: 100,
    name: "自定义任务5",
    drag: false,
    dropInner: false,
    nocheck: true
  },
];

//初始化加载数据
var zNodes1 = [

  {
    id: 11,
    pId: 0,
    name: "OKR推行",
    open: true,
    drop: true,
    dropInner: false,
    childOuter: false,
    nocheck: true
  },
  {
    id: 111,
    pId: 11,
    name: "每个季度OKR辅导",
    dropInner: false,
    nocheck: true
  },

  {
    id: 12,
    pId: 0,
    name: "明确线索信息",
    open: true,
    drop: true,
    dropInner: false,
    childOuter: false,
    nocheck: true
  },
  {
    id: 121,
    pId: 12,
    name: "获取线索信息和客户诉求",
    dropInner: false,
    nocheck: true
  },
  {
    id: 122,
    pId: 12,
    name: "获取客户计划和预算",
    dropInner: false,
    nocheck: true
  },
  {
    id: 123,
    pId: 12,
    name: "获取竞争信息",
    dropInner: false,
    nocheck: true
  },
  {
    id: 124,
    pId: 12,
    name: "获取项目关键决策链和关键人员",
    dropInner: false,
    nocheck: true
  },

];

// 拖拽前操作
function dropPrev(treeId, nodes, targetNode) {
  var pNode = targetNode.getParentNode();
  if (pNode && pNode.dropInner === false) {
    return false;
  } else {
    for (var i = 0, l = curDragNodes.length; i < l; i++) {
      var curPNode = curDragNodes[i].getParentNode();
      if (curPNode && curPNode !== targetNode.getParentNode() && curPNode.childOuter === false) {
        return false;
      }
    }
  }
  return true;
}

// 禁止拖拽成为目标节点的子节点
function dropInner(treeId, nodes, targetNode) {
  if (targetNode && targetNode.dropInner === false) {
    return false;
  } else {
    for (var i = 0, l = curDragNodes.length; i < l; i++) {
      if (!targetNode && curDragNodes[i].dropRoot === false) {
        return false;
      } else if (curDragNodes[i].parentTId && curDragNodes[i].getParentNode() !== targetNode && curDragNodes[i].getParentNode().childOuter === false) {
        return false;
      }
    }
  }
  return true;
}

// 禁止拖拽到节点后面的操作
function dropNext(treeId, nodes, targetNode) {
  var pNode = targetNode.getParentNode();
  if (pNode && pNode.dropInner === false) {
    return false;
  } else {
    for (var i = 0, l = curDragNodes.length; i < l; i++) {
      var curPNode = curDragNodes[i].getParentNode();
      if (curPNode && curPNode !== targetNode.getParentNode() && curPNode.childOuter === false) {
        return false;
      }
    }
  }
  return true;
}

var log, className = "dark",
  curDragNodes, autoExpandNode;

//禁止全部拖拽操作
function beforeDrag(treeId, treeNodes) {
  className = (className === "dark" ? "" : "dark");
  for (var i = 0, l = treeNodes.length; i < l; i++) {
    if (treeNodes[i].drag === false) {
      curDragNodes = null;
      return false;
    } else if (treeNodes[i].parentTId && treeNodes[i].getParentNode().childDrag === false) {
      curDragNodes = null;
      return false;
    }
  }
  curDragNodes = treeNodes;
  return true;
}

//禁止全部拖拽时的自动展开操作
function beforeDragOpen(treeId, treeNode) {
  autoExpandNode = treeNode;
  return true;
}

// 禁止将节点拖拽成为根节点
function beforeDrop(treeId, treeNodes, targetNode, moveType, isCopy) {
  className = (className === "dark" ? "" : "dark");
  return true;
}

// 每次开始进行拖拽节点后， 弹出被拖拽节点的个数信息
function onDrag(event, treeId, treeNodes) {
  className = (className === "dark" ? "" : "dark");
}

// 每次拖拽操作结束后， 弹出该被拖拽节点的个数以及目标节点的 tId、name 的信息
function onDrop(event, treeId, treeNodes, targetNode, moveType, isCopy) {
  className = (className === "dark" ? "" : "dark");
}

// 每次展开节点后， 弹出该节点的 tId、name 的信息
function onExpand(event, treeId, treeNode) {
  if (treeNode === autoExpandNode) {
    className = (className === "dark" ? "" : "dark");
  }
}

// 触发checked属性
function setTrigger() {
  var zTree = $.fn.zTree.getZTreeObj("treeDatas");
  zTree.setting.edit.drag.autoExpandTrigger = $("#callbackTrigger").attr("checked");
}

// 节点选中效果
function beforeClick(treeId, treeNode, clickFlag) {
  if (treeNode.pId == null) {
    // console.log("目标节点");
    if (treeNode.id < 100) {
      // console.log("标准目标节点");
      $('#edit').css("visibility", "hidden");
      $('#addLeaf').css("visibility", "hidden");
      return;
    } else {
      // console.log("自定义目标节点");
      $('#edit').css("visibility", "visible");
      $('#addLeaf').css("visibility", "visible");
      return;
    }
    return;
  }
  if (treeNode.pId < 100) {
    // console.log("标准任务节点");
    $('#edit').css("visibility", "hidden");
    $('#addLeaf').css("visibility", "hidden");
    return;
  } else {
    // console.log("自定义任务节点");
    $('#edit').css("visibility", "visible");
    $('#addLeaf').css("visibility", "hidden");
    return;
  }
}

//编辑输入验证方法
function beforeRename(treeId, treeNode, newName) {
  // console.log(treeNode);
  if (newName.length == 0) {
    layer.msg("节点名称不能为空.");
    var zTree = $.fn.zTree.getZTreeObj("treeDemo1");
    setTimeout(function () {
      zTree.editName(treeNode)
    }, 10);
    return false;
  }
  return true;
}

//编辑方法
function edit() {
  var zTree = $.fn.zTree.getZTreeObj("treeDemo1"),
    nodes = zTree.getSelectedNodes(),
    treeNode = nodes[0];
  if (nodes.length == 0) {
    layer.msg("请先选择一个自定义节点");
    return;
  }
  if (treeNode.pId >= 99) {
    zTree.editName(treeNode);
    return;
  }
  if (treeNode.drop && treeNode.id > 99) {
    zTree.editName(treeNode);
    return;
  } else {
    layer.msg("标准模板禁止编辑");
    return;
  }
};

//新增自定义子节点
function add(e) {
  var zTree = $.fn.zTree.getZTreeObj("treeDemo1"),
    isParent = e.data.isParent,
    nodes = zTree.getSelectedNodes(),
    treeNode = nodes[0];
  if (nodes.length == 0) {
    layer.msg("请先选择一个自定义目标节点");
    return;
  }
  if (nodes[0].pId == "" || nodes[0].pId == null || nodes[0].pId == 0) {
    if (nodes[0].id > 99) {
      if (nodes[0].children.length >= 10) {
        layer.msg("已超出自定义任务节点添加上限！");
        return;
      }
      if (nodes[0].children.length > 0 && nodes[0].children.length < 10) {
        var getAid = [];
        var maxAid;
        var cData = treeNode.children;
        for (var i = 0; i < cData.length; i++) {
          getAid.push(cData[i].id);
        }
        maxAid = Math.max.apply(null, getAid);
        zTree.addNodes(treeNode, {
          id: maxAid++,
          pId: nodes[0].id,
          name: "自定义任务节点",
          dropInner: false,
          nocheck: true
        });
        layer.msg("添加成功");
        return;
      }
      if (nodes[0].children.length == 0) {
        zTree.addNodes(treeNode, {
          id: 100,
          pId: nodes[0].id,
          name: "自定义任务节点",
          dropInner: false,
          nocheck: true
        });
        layer.msg("添加成功");
        return;
      }
    } else {
      layer.msg("请从左侧勾选对应模板导入");
      return;
    }
  } else {
    layer.msg("任务节点被锁定，禁止添加自定义节点");
    return;
  }
};

//删除方法
function remove(e) {
  var zTree = $.fn.zTree.getZTreeObj("treeDemo1"),
    nodes = zTree.getSelectedNodes(),
    treeNode = nodes[0];
  if (nodes.length == 0) {
    layer.msg("请先选择一个节点");
    return;
  }
  var callbackFlag = $("#callbackTrigger").attr("checked");
  layer.confirm('确认删除当前节点？', {
    btn: ['确认', '取消']
  }, function () {
    zTree.removeNode(treeNode, callbackFlag);
    layer.msg('删除成功', {
      icon: 1
    });
  });
};

//展开折叠
function expandNode(e) {
  var zTree = $.fn.zTree.getZTreeObj("treeDatas"),
    type = e.data.type,
    nodes = zTree.getSelectedNodes();
  if (type.indexOf("All") < 0 && nodes.length == 0) {
    layer.msg("请先选择一个父节点");
  }
  if (type == "expandAll") {
    zTree.expandAll(true);
  } else if (type == "collapseAll") {
    zTree.expandAll(false);
  } else {
    var callbackFlag = $("#callbackTrigger").attr("checked");
    for (var i = 0, l = nodes.length; i < l; i++) {
      zTree.setting.view.fontCss = {};
      if (type == "expand") {
        zTree.expandNode(nodes[i], true, null, null, callbackFlag);
      } else if (type == "collapse") {
        zTree.expandNode(nodes[i], false, null, null, callbackFlag);
      } else if (type == "toggle") {
        zTree.expandNode(nodes[i], null, null, null, callbackFlag);
      } else if (type == "expandSon") {
        zTree.expandNode(nodes[i], true, true, null, callbackFlag);
      } else if (type == "collapseSon") {
        zTree.expandNode(nodes[i], false, true, null, callbackFlag);
      }
    }
  }
}

//加载时执行事件绑定
$(document).ready(function () {
  $.fn.zTree.init($("#treeDatas"), setting, zNodes);
  $.fn.zTree.init($("#treeDemo1"), setting, zNodes1);
  $("#callbackTrigger").bind("change", {}, setTrigger);
  $("#edit").bind("click", edit);
  $("#remove").bind("click", remove);
  $("#expandAllBtn").bind("click", {
    type: "expandAll"
  }, expandNode);
  $("#collapseAllBtn").bind("click", {
    type: "collapseAll"
  }, expandNode);
  $("#addLeaf").bind("click", {
    isParent: false
  }, add);
});

// 导入动作
$(".getcheck").on('click', function () {
  // 获取左侧勾选参数
  var zTreeOjb = $.fn.zTree.getZTreeObj("treeDatas").getCheckedNodes();
  // 获取右侧原始数据
  var zData = $.fn.zTree.getZTreeObj("treeDemo1").getNodes();
  // 右侧数据
  var zTree = $.fn.zTree.getZTreeObj("treeDemo1");
  // 声明变量—空数组
  var getMax = [];
  var maxID;
  // 获取原始数据节点ID的最大值
  if (zData.length == 0) {
    // console.log("null")
  } else {
    for (var i = 0; i < zData.length; i++) {
      getMax.push(zData[i].id);
    }
    maxID = Math.max.apply(null, getMax);
    if (maxID < 100) {
      maxID = 100;
    }
    // console.log(maxID);
  }
  // 判断是否勾选左侧参数
  if (zTreeOjb.length != 0) {
    // console.log(zTreeOjb);
    for (var i = 0; i < zTreeOjb.length; i++) {
      if (zTreeOjb[i].pId < 10) {
        // console.log(zTreeOjb[i].id);
        var shNumber = $.inArray(zTreeOjb[i].id, getMax);
        if (shNumber == -1) {
          // console.log("不存在");
          //右侧新增目标节点
          zTree.addNodes(0, {
            id: zTreeOjb[i].id,
            pId: 0,
            name: zTreeOjb[i].name,
            open: true,
            drop: true,
            dropInner: false,
            childOuter: false,
            nocheck: true
          });
          var childData = zTreeOjb[i].children;
          var node = zTree.getNodeByParam("id", zTreeOjb[i].id);
          //先取消所有的选中状态
          zTree.cancelSelectedNode();
          //将指定ID的节点选中
          zTree.selectNode(node, true);
          var nodes = zTree.getSelectedNodes();
          var treeNode = nodes[0];
          for (var j = 0; j < childData.length; j++) {
            zTree.addNodes(treeNode, {
              id: childData[j].id,
              pId: zTreeOjb[i].id,
              name: childData[j].name,
              dropInner: false,
              nocheck: true
            });
          }
        } else {
          // console.log("已存在");
          var childData = zTreeOjb[i].children;
          var node = zTree.getNodeByParam("id", zTreeOjb[i].id);
          //先取消所有的选中状态
          zTree.cancelSelectedNode();
          //将指定ID的节点选中
          zTree.selectNode(node, true);
          var nodes = zTree.getSelectedNodes();
          // console.log(nodes);
          //清空选中子节点数据
          zTree.removeChildNodes(nodes[0]);

          var nodesChildren = nodes[0].children;
          // console.log(nodesChildren);

          var treeNode = nodes[0];
          // console.log(treeNode);

          for (var j = 0; j < childData.length; j++) {
            zTree.addNodes(treeNode, {
              id: childData[j].id,
              pId: childData[j].pId,
              name: childData[j].name,
              dropInner: false,
              nocheck: true
            });
          }
        }
      }
      // 目标节点判断
      if (zTreeOjb[i].pId >= 99) {
        if (maxID == undefined || maxID == null || maxID == "" || maxID == 0) {
          maxID = 100;
        }
        if (maxID >= 110) {
          layer.msg("已超出自定义目标节点添加上限！");
          return;
        } else {
          zTree.addNodes(0, {
            id: (maxID + 1),
            pId: 0,
            name: zTreeOjb[i].name,
            open: true,
            drop: true,
            dropInner: false,
            childOuter: false,
            nocheck: true
          });
          maxID++;
          var childData = zTreeOjb[i].children;
          // console.log(childData);
          var node = zTree.getNodeByParam("id", maxID);
          // console.log(node);
          //先取消所有的选中状态
          zTree.cancelSelectedNode();
          //将指定ID的节点选中
          zTree.selectNode(node, true);
          // 获取选择节点数据
          var nodes = zTree.getSelectedNodes();
          var treeNode = nodes[0];
          for (var j = 0; j < childData.length; j++) {
            zTree.addNodes(treeNode, {
              id: (maxID + 100 + j),
              pId: maxID,
              name: childData[j].name,
              dropInner: false,
              nocheck: true
            });
          }
        }
      }
    };
    // 将取消节点选中状态
    zTree.cancelSelectedNode();
    layer.msg("导入成功！");
  } else {
    layer.msg("请先勾选左侧需要的标准节点！");
  }
})
//全选
$(".getall").on('click', function () {
  var treeObj = $.fn.zTree.getZTreeObj("treeDatas");
  treeObj.checkAllNodes(true);
});
//取消全选
$(".delall").on('click', function () {
  var treeObj = $.fn.zTree.getZTreeObj("treeDatas");
  treeObj.checkAllNodes(false);
});

$(".t-close").on('click', function () {
  layer.confirm('确认关闭此窗口？', {
    btn: ['确认', '取消'] //按钮
  }, function () {
    var index = parent.layer.getFrameIndex(window.name);
    parent.layer.close(index);
  });
});
// 提交标准模板数据
$(".submit").on("click", function () {
  var zTreeOjb = $.fn.zTree.getZTreeObj("treeDemo1").getNodes();
  if (zTreeOjb.length != 0) {
    //数据结果集
    console.log(zTreeOjb);
    layer.msg("提交成功！");
  } else {
    layer.msg("没有需要提交的数据！");
  }
})

$("#helpme").on('click', function () {
  //示范一个公告层
  layer.open({
    type: 1,
    skin: 'layui-layer-lan',
    title: false //不显示标题栏
      ,
    closeBtn: false,
    area: '800px;',
    shade: 0.3,
    id: 'help' //设定一个id，防止重复弹出
      ,
    resize: false,
    btn: '明白了',
    btnAlign: 'c',
    moveType: 1 //拖拽模式，0或者1
      ,
    content: '<div style="padding: 50px; line-height: 26px; background-color: #393D49;font-size:14px; color: #fff; font-weight: 300;">' +
      '<h5 style="text-align:center;font-size:18px;">帮助文档</h5><br/>' +
      '【新建标准目标+标准任务】<br/>' +
      '&nbsp;&nbsp;从左侧绿色区域勾选后，点击导入动作按钮（标准节点只允许导入一次，多次导入无效）；<br/>' +
      '【节点裁剪】<br/>' +
      '&nbsp;&nbsp;没有子任务进展数据录入的前提下，可以进行任意节点裁剪删除，已填写数据情况除外！<br/>' +
      '【标准节点更新】<br/>' +
      '&nbsp;&nbsp;已删除的标准目标节点/标准任务节点，可以再次从左侧绿色区域勾选导入；<br/>' +
      '【自定义节点导入】<br/>' +
      '&nbsp;&nbsp;自定义节点，可以从左侧绿色区域勾选，最多允许导入10个自定义标准目标节点；<br/>' +
      '【自定义节点操作】<br/>' +
      '&nbsp;&nbsp;自定义目标节点+自定义任务节点，可以进行编辑、删除、新增自定义任务节点操作。<br/>' +
      '（注：自定义任务节点允许添加上限5个，在新增前，请先从右侧绿色区域点击选中需要关联的自定义目标节点数据，然后点击最右侧添加自定义任务按钮即可）。' +
      '</div>'
    // , success: function (layero) {
    //   var btn = layero.find('.layui-layer-btn');
    //   btn.find('.layui-layer-btn0').attr({
    //     href: 'http://www.layui.com/'
    //     , target: '_blank'
    //   });
    // }
  });
});